package fashionshop.gui.common.component;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import fashionshop.gui.common.model.ModelData;

@SuppressWarnings("serial")
public class GridViewTableModel extends AbstractTableModel {
	
	private List<ModelData> data;
	private List<ColumnConfig> columns;
	
	public GridViewTableModel(){
		this(null, null);
	}
	
	public GridViewTableModel(List<ModelData> data, List<ColumnConfig> columns){
		setTableData(data, columns);
	}
	
	public void addRow(ModelData model){
		insertRow(model, getRowCount());
	}
	
	public void insertRow(ModelData model, int row){
		data.add(row, model);
		fireTableRowsInserted(row, row);
	}
	
	public void addRows(List<ModelData> models){
		insertRows(models, getRowCount());
	}
	
	public void insertRows(List<ModelData> models, int row){
		data.addAll(row, models);
		fireTableRowsInserted(row, models.size());
	}
	
	public void updateRow(ModelData oldModel, ModelData newModel){
		int row = findModel(oldModel);
		updateRow(newModel, row);
	}
	
	public void updateRow(ModelData model, int row){
		data.set(row, model);
		fireTableRowsUpdated(row, row);
	}
	
	public int findModel(ModelData model){
		return data.indexOf(model);
	}
	
	public ModelData getModel(int row){
		return data.get(row);
	}
	
	public void removeRow(int row){
		data.remove(row);
		fireTableRowsDeleted(row, row);
	}
	
	public void removeRow(ModelData model){
		int row = findModel(model);
		if(row >= 0){
			removeRow(row);
		}
	}
	
	public void removeAll(){
		int numRow = getRowCount();
		data.clear();
		fireTableRowsDeleted(0, numRow);
	}
	
	public void setTableData(List<ModelData> data, List<ColumnConfig> columns){
		if(data == null){
			data = new ArrayList<ModelData>();
		}
		if(columns == null){
			columns = new ArrayList<ColumnConfig>();
		}
		this.setData(data);
		this.setColumns(columns);
		fireTableStructureChanged();
	}
	
	@Override
	public int getColumnCount() {
		return getColumns().size();
	}

	@Override
	public int getRowCount() {
		return getData().size();
	}

	@Override
	public Object getValueAt(int row, int col) {
		ColumnConfig colConf = getColumns().get(col);
		ModelData model = getData().get(row);
		return model.get(colConf.getId());
	}
	
	/**
	 * Return column heading
	 */
	@Override
	public String getColumnName(int column) {
		return getColumns().get(column).getHeading();
	}
	
	/**
	 * Find column by ID
	 */
	@Override
	public int findColumn(String columnId) {
		for(int i = 0; i < getColumnCount(); i++){
			ColumnConfig colConf = getColumns().get(i);
			if(columnId.equals(colConf.getId())){
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * Disable edit
	 */
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		return false;
	}

	public List<ModelData> getData() {
		return data;
	}

	public void setData(List<ModelData> data) {
		this.data = data;
		fireTableDataChanged();
	}

	public List<ColumnConfig> getColumns() {
		return columns;
	}

	public void setColumns(List<ColumnConfig> columns) {
		this.columns = columns;
		fireTableStructureChanged();
	}

}
